package com.wocao.utils;

import com.wocao.entity.SysMenu;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class dynamicMenu {

    /**
     * 递归查找子菜单
     *
     * @param id      当前菜单ID
     * @param allList 查找菜单列表
     * @return
     */
    public static List<SysMenu> getChild(Integer id, List<SysMenu> allList) {
        // 子菜单
        List<SysMenu> childList = new ArrayList<>();
        for (SysMenu menu : allList) {
            if (menu.getParentId().equals(id)) {
                childList.add(menu);
            }
        }

        // 为子菜单设置子菜单
        for (SysMenu nav : childList) {
            nav.setChildMenu(getChild(Math.toIntExact(nav.getMenuId()), allList));
        }

        if (childList.size() == 0) {
            return new ArrayList<>();
        }

        // 排序
        childList = childList.stream()
                .sorted(Comparator.comparing(SysMenu::getOrderNum))
                .collect(Collectors.toList());

        return childList;
    }
}
